package com.focusai.efairy.utils;

import android.graphics.Bitmap;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class IOUtil {
	
	public static void write(InputStream input, OutputStream output) throws IOException {
		byte[] bs = new byte[1024];
		while (input.read(bs) != -1) {
			output.write(bs);
		}
		output.flush();
		output.close();
		input.close();
	}
	
	public static void copy(InputStream input, OutputStream output) throws IOException {
		write(input, output);
	}
	
	public static void write(InputStream input, String pathName) throws IOException {
		OutputStream output = new FileOutputStream(pathName);
		write(input, output);
	}
	
	/**
	 * 压缩比50%
	 * 格式JPG
	 * @param bm
	 * @return
	 */
	public static InputStream bitmap2InputStream(Bitmap bm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 50, baos);
        InputStream is = new ByteArrayInputStream(baos.toByteArray());
        return is;
    }
	
	public static void write(Bitmap bitmap, String pathName) throws IOException {
		write(bitmap2InputStream(bitmap), pathName);
	}
	
}
